home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / ARTAbrot 1.1 Source / ARTAbrot.c < prev    next >
Text File  |  1993-11-22  |  10KB  |  280 lines

  1. /************************************************************************/
  2. /*                                                                        */
  3. /*    FILE:        ARTAbrot.c -- Main windows code for ARTA Mandelbrot        */
  4. /*                    program.                                            */
  5. /*                                                                        */
  6. /*    PURPOSE:    Display Mandelbrot graphics.                            */
  7. /*                                                                        */
  8. /*    AUTHOR:        George T. Warner                                        */
  9. /*                                                                        */
  10. /*    REVISIONS:                                                            */
  11. /*    08/22/93    First version.                                            */
  12. /*                                                                        */
  13. /************************************************************************/
  14.  
  15. #include "ComUtil_ARTAbrot.h"    /* Common */
  16.  
  17. #include "AB_Alert.h"    /* Alert */
  18. #include "ARTAbrot.h"    /* This file */
  19. #include "brotcode.h"
  20.  
  21. #pragma segment ARTAbrot
  22.  
  23. static ControlHandle    ScrollHHandle;        /* Scrollbar for horz scrolling */
  24. static ControlHandle    ScrollVHandle;        /* Scrollbar for vert scrolling */
  25. static RGBColor    WindowBackColor;            /* Background window color */
  26. /* Offscreen pixMap stuff. */
  27. GWorldPtr offscreenGWorld = NIL;    /* Off screen graphics world. */
  28. Rect    GWorldBounds;            /* Boundaries of graphics world. */
  29.  
  30.  
  31. /* Routine: Init_ARTAbrot */
  32. /* Purpose: Initialize our window data to not in use yet */
  33.  
  34. void Init_ARTAbrot()
  35. {
  36.     WPtr_ARTAbrot = nil;                    /* Make sure other routines know we are not valid yet */
  37.  
  38.     ScrollHHandle = nil;                    /* Scrollbar is not valid yet */
  39.     ScrollVHandle = nil;                    /* Scrollbar is not valid yet */
  40.  
  41.     WindowBackColor.red  = 0xFFFF;  WindowBackColor.green  = 0xFFFF;  WindowBackColor.blue  = 0xFFFF;/* Set the color */
  42. }
  43.  
  44. /* ======================================================= */
  45.  
  46. /* Routine: Close_ARTAbrot */
  47. /* Purpose: Close out the window */
  48.  
  49. void Close_ARTAbrot(WindowPtr whichWindow)
  50. {
  51.     /* See if we should close this window */
  52.     if ((WPtr_ARTAbrot != NIL) && ((WPtr_ARTAbrot == whichWindow) || (whichWindow == (WindowPtr)-1))) {
  53.         DisposeWindow(WPtr_ARTAbrot);        /* Clear window and controls */
  54.         WPtr_ARTAbrot = nil;                /* Make sure other routines know we are closed */
  55.         DisposeGWorld(offscreenGWorld);
  56.         offscreenGWorld = NIL;
  57.     }
  58. }
  59.  
  60. /* ======================================================= */
  61.  
  62. /* Routine: Resized_ARTAbrot */
  63. /* Purpose: We were resized or zoomed, update the scrolling scrollbars */
  64.  
  65. void Resized_ARTAbrot(WindowPtr whichWindow)
  66. {
  67. WindowPtr    SavePort;                        /* Place to save the last port */
  68. Rect    temp2Rect;                            /* temp rectangle */
  69. QDErr result;
  70. long row_bytes;    /* Used for PixMap calculations. */
  71. long bytes2erase;
  72. unsigned char *base_addr;
  73.  
  74.     if (WPtr_ARTAbrot == whichWindow) {        /* Only do if the window is us */
  75.         GetPort(&SavePort);                    /* Save the current port */
  76.         SetPort(WPtr_ARTAbrot);                /* Set the port to my window */
  77.     
  78.         temp2Rect = WPtr_ARTAbrot->portRect;    /* Get the window rectangle */
  79.         EraseRect(&temp2Rect);                    /* Erase the new window area */
  80.         InvalRect(&temp2Rect);                    /* Set to update the new window area */
  81.  
  82.         /* Dispose old GWorld. */
  83.         DisposeGWorld(offscreenGWorld);
  84.         offscreenGWorld = NIL;
  85.         /* Allocate memory for offscreen bitmap. */
  86.         /* left, top, right, bottom */
  87.         SetRect(&GWorldBounds, 0, 0, (WPtr_ARTAbrot->portRect.right - WPtr_ARTAbrot->portRect.left)-SCROLLBARWIDTH, (WPtr_ARTAbrot->portRect.bottom - WPtr_ARTAbrot->portRect.top)-SCROLLBARWIDTH);
  88.         result = NewGWorld(&offscreenGWorld, 8, &GWorldBounds, NIL, NIL, NIL);
  89.         if (result) {
  90.             DisposeWindow(WPtr_ARTAbrot); 
  91.             WPtr_ARTAbrot = NIL;
  92.             /* Report an error message here. */
  93.         }
  94.         else {
  95.             /* First clear the offscreen GWorld. */
  96.             /* Lock the offscreen GWorld. */
  97.             LockPixels(offscreenGWorld->portPixMap);
  98.             row_bytes = ((**(offscreenGWorld->portPixMap)).rowBytes) & 0x3fff;
  99.             base_addr = (unsigned char *)GetPixBaseAddr(offscreenGWorld->portPixMap);
  100.             bytes2erase = row_bytes * GWorldBounds.bottom;
  101.             do {
  102.                 *base_addr++ = 0;
  103.             } while (--bytes2erase);
  104.             UnlockPixels(offscreenGWorld->portPixMap);
  105.             start_brot();                    /* Calculate new Mandelbrot. */
  106.         }
  107.     
  108.         SetPort(SavePort);                                /* Restore the old port */
  109.     }
  110. }
  111.  
  112. /* ======================================================= */
  113.  
  114. /* Routine: Moved_ARTAbrot */
  115. /* Purpose: We were moved, possibly to another screen and screen depth */
  116.  
  117. void Moved_ARTAbrot(WindowPtr whichWindow)
  118. {
  119. WindowPtr    SavePort;                        /* Place to save the last port */
  120.     
  121.     if (WPtr_ARTAbrot == whichWindow) {        /* Only do if the window is us */
  122.         GetPort(&SavePort);                    /* Save the current port */
  123.         SetPort(WPtr_ARTAbrot);                /*  Set the port to my window  */
  124.     
  125.         SetPort(SavePort);                                /* Restore the old port */
  126.     }
  127. }
  128.  
  129. /* ======================================================= */
  130.  
  131. /* Routine: Update_ARTAbrot */
  132. /* Purpose: Update our window */
  133.  
  134. void Update_ARTAbrot(WindowPtr whichWindow)
  135. {
  136. WindowPtr    SavePort;                                /* Place to save the last port */
  137.  
  138.     /* Handle the update to our window */
  139.     if ((WPtr_ARTAbrot != NIL) && (WPtr_ARTAbrot == whichWindow)) {
  140.         GetPort(&SavePort);                    /* Save the current port */
  141.         SetPort(WPtr_ARTAbrot );            /* Set the port to my window */
  142.  
  143.         DrawControls(WPtr_ARTAbrot);        /* Draw all the controls */
  144.         DrawGrowIcon(WPtr_ARTAbrot );        /* Draw the Grow box */
  145.         DrawControls(WPtr_ARTAbrot);        /* Draw all the controls */
  146.         /* Restore image. */
  147.         if (LockPixels(offscreenGWorld->portPixMap)) {
  148.             CopyBits((BitMap *)*offscreenGWorld->portPixMap, &(WPtr_ARTAbrot->portBits), &(GWorldBounds), &(GWorldBounds), srcCopy, 0);
  149.             UnlockPixels(offscreenGWorld->portPixMap);
  150.         }
  151.         SetPort(SavePort);                    /* Restore the old port */
  152.     }
  153. }
  154.  
  155. /* ======================================================= */
  156.  
  157. /* Routine: Open_ARTAbrot */
  158. /* Purpose: Open our window */
  159.  
  160. void Open_ARTAbrot()
  161. {
  162. QDErr result;
  163. long row_bytes;    /* Used for PixMap calculations. */
  164. long bytes2erase;
  165. unsigned char *base_addr;
  166.  
  167.     if (WPtr_ARTAbrot == NIL) {                    /* See if already opened */
  168.         WPtr_ARTAbrot = GetNewCWindow(Res_W_ARTAbrot,nil, (WindowPtr)-1);/* Get the COLOR window from the resource file */
  169.         SetPort(WPtr_ARTAbrot);                    /* Prepare to write into our window */
  170.  
  171.         ShowWindow(WPtr_ARTAbrot);                /* Show the window now */
  172.         /* Allocate memory for offscreen bitmap. */
  173.         /* left, top, right, bottom */
  174.         SetRect(&GWorldBounds, 0, 0, (WPtr_ARTAbrot->portRect.right - WPtr_ARTAbrot->portRect.left)-SCROLLBARWIDTH, (WPtr_ARTAbrot->portRect.bottom - WPtr_ARTAbrot->portRect.top)-SCROLLBARWIDTH);
  175.         result = NewGWorld(&offscreenGWorld, 8, &GWorldBounds, NIL, NIL, NIL);
  176.         if (result) {
  177.             DisposeWindow(WPtr_ARTAbrot); 
  178.             WPtr_ARTAbrot = NIL;
  179.             /* Report an error message here. */
  180.         }
  181.         else {
  182.             /* First clear the offscreen GWorld. */
  183.             /* Lock the offscreen GWorld. */
  184.             LockPixels(offscreenGWorld->portPixMap);
  185.             row_bytes = ((**(offscreenGWorld->portPixMap)).rowBytes) & 0x3fff;
  186.             base_addr = (unsigned char *)GetPixBaseAddr(offscreenGWorld->portPixMap);
  187.             bytes2erase = row_bytes * GWorldBounds.bottom;
  188.             do {
  189.                 *base_addr++ = 0;
  190.             } while (--bytes2erase);
  191.             UnlockPixels(offscreenGWorld->portPixMap);
  192.  
  193.             start_brot();                    /* Calculate new Mandelbrot. */
  194.         }
  195.         SelectWindow(WPtr_ARTAbrot);        /* Already open, so show it */
  196.     }
  197.     else {
  198.         SelectWindow(WPtr_ARTAbrot);         /* Already open, so show it */
  199.         if (new_coordinates)
  200.             start_brot();                    /* Calculate new Mandelbrot. */
  201.         else {
  202.             /* Restore image. */
  203.             if (LockPixels(offscreenGWorld->portPixMap)) {
  204.                 CopyBits((BitMap *)*offscreenGWorld->portPixMap, &(WPtr_ARTAbrot->portBits), &(GWorldBounds), &(GWorldBounds), srcCopy, 0);
  205.                 UnlockPixels(offscreenGWorld->portPixMap);
  206.             }
  207.         }
  208.     }
  209. }
  210.  
  211. /* ======================================================= */
  212.  
  213. /* Routine: Activate_ARTAbrot */
  214. /* Purpose: We activated or deactivated */
  215.  
  216. void Activate_ARTAbrot(WindowPtr whichWindow,Boolean Do_An_Activate)
  217. {
  218. WindowPtr    SavePort;                                /* Place to save the last port */
  219.  
  220. if (WPtr_ARTAbrot == whichWindow)                    /*  Only do if the window is us  */
  221.     {
  222.     GetPort(&SavePort);                            /* Save the current port */
  223.     SetPort(whichWindow);                            /* Set the port to my window */
  224.  
  225.     DrawGrowIcon(whichWindow);                        /* Draw the grow Icon */
  226.  
  227.     if (Do_An_Activate)                            /* Handle the activate */
  228.         {
  229.         }
  230.     else
  231.         {
  232.         if (theInput != nil)                        /* See if there is already a TE area */
  233.             {
  234.             TEDeactivate(theInput);                /* Yes, so turn it off */
  235.             }                                    /* End of IF */
  236.         theInput = nil;                            /* Deactivate the TE area */
  237.         }                                        /* End of IF */
  238.  
  239.     SetPort(SavePort);                                /* Restore the old port */
  240.     }                                            /* End of IF */
  241. }
  242.  
  243. /* ======================================================= */
  244.  
  245. /* Routine: Do_ARTAbrot */
  246. /* Purpose: Handle action to our window, like controls */
  247.  
  248. void Do_ARTAbrot(EventRecord *myEvent)
  249. {
  250. short    code;                                        /* Location of event in window or controls */
  251. WindowPtr    whichWindow;                            /* Window pointer where event happened */
  252. Point    myPt;                                        /* Point where event happened */
  253. ControlHandle    theControl;                        /* Handle for a control */
  254. code = FindWindow(myEvent->where, &whichWindow);    /* Get where in window and which window */
  255.  
  256. if (WPtr_ARTAbrot == whichWindow)                    /* Handle only when the window is valid */
  257.     {
  258.     code = FindWindow(myEvent->where, &whichWindow);/* Get where in window and which window */
  259.  
  260.     if ((myEvent->what == mouseDown) && (WPtr_ARTAbrot == whichWindow))
  261.         {
  262.         myPt = myEvent->where;                        /* Get mouse position */
  263.         GlobalToLocal(&myPt);                        /* Make it relative */
  264.  
  265.         }                                        /* End of IF */
  266.  
  267.     if ((WPtr_ARTAbrot == whichWindow) && (code == inContent))/* for our window */
  268.         {
  269.  
  270.         code = FindControl(myPt, whichWindow, &theControl);/* Get type of control */
  271.  
  272.         if (code != 0)                                /* Check type of control */
  273.             code = TrackControl(theControl,myPt, (ProcPtr)-1);/* Track the control */
  274.  
  275.         }
  276.     }
  277. }
  278.  
  279. /* ======================================================= */
  280.